<template>
  <div class="container">
    <el-dialog :title="title" :visible="isDialogShow" @close="handleClose">
      <el-form
        :rules="rules"
        ref="form"
        size="small"
        label-width="100px"
        :model="formData"
      >
        <el-form-item label="文章标题：" prop="title">
          <el-input
            v-model="formData.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody">
          <quill-editor
            v-model="formData.articleBody"
            :options="editorOptions"
            @blur="question()"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址：" prop="videoURL">
          <el-input
            v-model="formData.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="clickSubmit">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '@/api/hmmm/articles.js'
export default {
  components: { quillEditor },
  props: {
    isDialogShow: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    title () {
      return this.data.id ? '修改文章' : '新增文章'
    }
  },
  data () {
    return {
      formData: {
        title: null,
        articleBody: null,
        videoURL: null
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入文章标题',
            trigger: ['blur', 'change']
          }
        ],
        articleBody: [
          {
            required: true,
            message: '请输入文章内容',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    handleClose () {
      this.$emit('update:isDialogShow', false)
      this.$refs.form.resetFields()
      if (this.data.id) {
        this.formData = {
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
      this.currArt = {}
    },
    // 富文本校验
    question () {
      this.$refs.form.validateField('articleBody')
    },
    // 提交
    clickSubmit () {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return console.log('校验失败')

        if (this.data.id) {
          // 编辑
          this.$message.success('修改成功')
          this.formData.id = this.data.id
          await update(this.formData)
        } else {
          // 添加
          await add(this.formData)
          this.$message.success('添加成功')
        }
        this.handleClose()
        this.$parent.getArticlesList()
      })
    },
    // 打开对话框
    open () {
      if (this.data.id) {
        const { id, title, articleBody, videoURL } = this.data
        this.formData = { id, title, articleBody, videoURL }
      } else {
        this.formData = {
          id: null,
          title: null,
          articleBody: null,
          videoURL: null
        }
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container ::v-deep .ql-editor {
  height: 200px;
}
</style>
